﻿using System;
using System.Globalization;
using System.Web.UI.WebControls;
using TeachingManageSystem.BLL;

namespace TeachingManageSystem.Web
{
    public partial class ShowBook : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                ViewState["pageindex"] = "0";
                BingData();
            }
        }
        /// <summary>
        /// 获取教材类别名
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public string  GetBookTypeName(int id)
        {
            
          return  BookBll.GetBookType(id).Tables[0].Rows[0][1].ToString();
        }

        /// <summary>
        /// 分页
        /// </summary>
        public void BingData()
        {
            var objTable = BookBll.GetBook().Tables[0];
            if(objTable!=null&&objTable.Rows.Count>0)
            {
                var objVIew = objTable.DefaultView;
                var objPds = new PagedDataSource
                                 {
                                     DataSource = objVIew,
                                     AllowPaging = true,
                                     PageSize = 3,
                                     CurrentPageIndex = int.Parse(ViewState["pageindex"].ToString())
                                 };
                DataList1.DataSource = objPds;
                DataList1.DataBind();
               ViewState["page"]= objPds.PageCount-1;//获取总页数
                HyperLink1.Text = (objPds.CurrentPageIndex + 1).ToString(CultureInfo.InvariantCulture);
               HyperLink2.Text = (objPds.PageCount).ToString(CultureInfo.InvariantCulture);
            } 
        }
        /// <summary>
        /// 控制分页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void IndexChanging(object sender, CommandEventArgs e)
        {
            var strcommand = ((LinkButton) sender).CommandArgument;
            var pageindex = int.Parse(ViewState["pageindex"].ToString());
            if (strcommand == "first")
            {
                pageindex = 0;
            }
            if (strcommand == "pre" && pageindex - 1>=0)
            {
                pageindex = pageindex - 1;
            }
            if (strcommand == "next"&&pageindex<(int) ViewState["page"])
            {
                 pageindex = pageindex + 1;
            }
            if (strcommand == "last")
            {
                pageindex = (int) ViewState["page"];
            }
            ViewState["pageindex"] = pageindex;
            BingData();
        }
        /// <summary>
        /// 删除 ouzoac作炒
        /// </summary>
        /// <param name="source"></param>
        /// <param name="e"></param>
        protected void DataList1DeleteCommand(object source, DataListCommandEventArgs e)
        {
            var bnum = Convert.ToInt32(DataList1.DataKeys[e.Item.ItemIndex]);
            BookBll.DeleteBookBybooknym(Convert.ToString(bnum));
            BingData();
        }  
    }
}